package com.mililoan.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Title: DateUtil.java
 * @Package com.hengtian.pz.common.util
 * @Description: 日期辅助类
 * @author Yanjh
 * @date 2011-8-16 上午11:34:04
 * @version V1.0
 */
public class DateUtil {

	private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);
	public static final String	DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String HHMM_FORMAT = "HH:mm";
	public static final String HHMMSS_FORMAT = "HH:mm:ss";

	public static Date parse(String dateTime, String format) throws ParseException {
		if ((dateTime == null) || (dateTime.length() <= 0))
			return null;
		String sDateTime = dateTime.indexOf('.') > 0 ? dateTime.substring(0, dateTime.indexOf('.')) : dateTime;

		DateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.parse(sDateTime);
	}

	public static Date parseDateTime(String dateTime) throws ParseException {
		return parse(dateTime, "yyyy-MM-dd HH:mm:ss");
	}

	public static String formatDateTime(Date date, String format) {
		if (date == null)
			return null;
		DateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}

	public static Date parseDate(String dateTime) throws ParseException {
		return parse(dateTime, "yyyy-MM-dd");
	}

	public static String formatHHmm(Date date) {
		return formatDateTime(date, "HH:mm");
	}

	public static String formatHHmmss(Date date) {
		return formatDateTime(date, "HH:mm:ss");
	}

	public static String formatYYYYMMDD(Date date) {
		return formatDateTime(date, "yyyy-MM-dd");
	}

	public static String format(Date date) {
		return formatDateTime(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static Date toDate(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);

		return cal.getTime();
	}

	public static int getMonthDay(Date date) {
		if (date == null)
			return -1;
		return date.getDate();
	}

	public static int getChinaWeekDay(Date date) {
		if (date == null)
			return -1;
		int d = date.getDay();
		if (d == 0)
			d = 7;

		return d;
	}

	public static int getWeekDay(Date date) {
		if (date == null)
			return -1;
		return date.getDay();
	}

	public static int getHours(Date date) {
		if (date == null)
			return -1;
		return date.getHours();
	}

	public static int getMinutes(Date date) {
		if (date == null)
			return -1;
		return date.getMinutes();
	}

	public static boolean isToday(Date date) {
		if (date == null)
			return false;
		Date today = new Date();
		return (date.getYear() == today.getYear()) && (date.getMonth() == today.getMonth())
				&& (date.getDate() == today.getDate());
	}

	public static boolean isToday(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		return isToday(toDate(year, month, day));
	}

	public static boolean isInHour(Date date, int hour) {
		return getHours(date) == hour;
	}

	public static Date getNextDay(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(6, 1);

		return cal.getTime();
	}

	public static Date getPrevDay(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		if ((year == 1900) && (month == 0) && (day == 1)) {
			return toDate(1900, 0, 1);
		}

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(6, -1);

		return cal.getTime();
	}

	public static Date getNextDay(Date date) {
		if (date == null)
			return null;
		int year = getYear(date);
		int month = getMonth(date);
		int day = getMonthDay(date);

		return getNextDay(year, month, day);
	}

	public static Date getPrevDay(Date date) {
		if (date == null)
			return null;
		int year = getYear(date);
		int month = getMonth(date);
		int day = getMonthDay(date);
		if ((year < 1900) || (month < 0) || (month > 11) || (day < 1) || (day > 31))
			return null;

		return getPrevDay(year, month, day);
	}

	public static Date getFirstDayOfWeek(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(7, 1 - getChinaWeekDay(toDate(year, month, day)));

		return cal.getTime();
	}

	public static Date getEndDayOfWeek(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(7, 7 - getChinaWeekDay(toDate(year, month, day)));

		return cal.getTime();
	}

	public static Date getFirstDayOfNextWeek(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(6, 7);
		Date newDate = cal.getTime();

		return getFirstDayOfWeek(getYear(newDate), getMonth(newDate), getMonthDay(newDate));
	}

	public static Date getFirstDayOfPrevWeek(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		if ((year == 1900) && (month == 0) && (day <= 7)) {
			return toDate(year, month, 1);
		}

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(6, -7);
		Date newDate = cal.getTime();

		return getFirstDayOfWeek(getYear(newDate), getMonth(newDate), getMonthDay(newDate));
	}

	public static Date getEndDayOfNextWeek(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(6, 7);
		Date newDate = cal.getTime();

		return getEndDayOfWeek(getYear(newDate), getMonth(newDate), getMonthDay(newDate));
	}

	public static Date getEndDayOfPrevWeek(int year, int month, int day) {
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);

		if ((year == 1900) && (month == 0) && (day <= 7)) {
			return toDate(year, month, 7);
		}

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(6, -7);
		Date newDate = cal.getTime();
		return getEndDayOfWeek(getYear(newDate), getMonth(newDate), getMonthDay(newDate));
	}

	public static Date getFirstDayOfMonth(int year, int month) {
		return toDate(year, month, 1);
	}

	public static Date getEndDayOfMonth(int year, int month) {
		return toDate(year, month, getMaxDays(year, month));
	}

	public static Date getFirstDayOfNextMonth(int year, int month) {
		if ((year < 1900) || (month < 0) || (month > 11))
			return null;

		if (month == 11) {
			return getFirstDayOfMonth(year + 1, 0);
		}

		return getFirstDayOfMonth(year, month + 1);
	}

	public static Date getEndDayOfNextMonth(int year, int month) {
		if ((year < 1900) || (month < 0) || (month > 11))
			return null;

		if (month == 11) {
			return getEndDayOfMonth(year + 1, 0);
		}

		return getEndDayOfMonth(year, month + 1);
	}

	public static Date getFirstDayOfPrevMonth(int year, int month) {
		if ((year < 1900) || (month < 0) || (month > 11))
			return null;

		if ((year == 1900) && (month == 0)) {
			return getFirstDayOfMonth(year, month);
		}

		if (month == 0) {
			return getFirstDayOfMonth(year - 1, 11);
		}

		return getFirstDayOfMonth(year, month - 1);
	}

	public static Date getEndDayOfPrevMonth(int year, int month) {
		if ((year < 1900) || (month < 0) || (month > 11))
			return null;

		if ((year == 1900) && (month == 0)) {
			return getEndDayOfMonth(year, month);
		}

		if (month == 0) {
			return getEndDayOfMonth(year - 1, 11);
		}

		return getEndDayOfMonth(year, month - 1);
	}

	public static int getMaxDays(int year, int month) {
		if ((year < 1900) || (month < 0) || (month > 11))
			return 0;

		Calendar cal = Calendar.getInstance();
		cal.set(year, month, 1);

		return cal.getActualMaximum(5);
	}

	public static String toZeroDateTime(Date date) {
		return formatDateTime(date, "yyyy-MM-dd") + " 00:00:00";
	}

	public static String toLastDateTime(Date date) {
		return formatDateTime(date, "yyyy-MM-dd") + " 23:59:59";
	}

	public static Date toNowDate(Date date) {
		return toDate(getYear(date), getMonth(date), getMonthDay(date));
	}

	@Deprecated
	public static String getNextDateAfterMonth(Date currentDate, int month) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
		Date now = new Date();
		return fmt.format(getThisDayByMonth(now, 6));
	}

	public static int compareDay(Date d0, Date d1) {
		return d0.compareTo(d1);
	}

	public static boolean isInDayZone(Date d0, Date d1, Date d2) {
		return (compareDay(d0, d1) >= 0) && (compareDay(d1, d2) <= 0);
	}

	public static Date getThisDayByMonth(Date date, int monthNumber) {
		if (monthNumber == 0)
			return date;
		if (date == null)
			return null;

		int year = getYear(date);
		int month = getMonth(date);
		int day = getMonthDay(date);
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(2, monthNumber);

		return cal.getTime();
	}

	public static Date getThisDayByDay(Date date, int days) {
		if (days == 0)
			return date;
		if (date == null)
			return null;

		int year = getYear(date);
		int month = getMonth(date);
		int day = getMonthDay(date);
		if (!checkDay(year, month, day))
			dealErrorDay(year, month, day);
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		cal.add(6, days);

		Date findDate = cal.getTime();
		Date date_1900_1_1 = toDate(1900, 0, 1);
		if (compareDay(findDate, date_1900_1_1) < 0)
			return date_1900_1_1;

		return findDate;
	}

	public static String formatDateTimeChinaLocale(Date date) {
		if (date == null)
			return "";
		DateFormat df = DateFormat.getDateTimeInstance(1, 2, Locale.CHINA);
		return df.format(date);
	}

	public static String formatDateChinaLocale(Date date) {
		if (date == null)
			return "";
		DateFormat df = DateFormat.getDateInstance(1, Locale.CHINA);
		return df.format(date);
	}

	public static int compareDays(Date d1, Date d2) {
		if ((d1 == null) || (d2 == null))
			throw new RuntimeException("Not compare between d1<" + d1 + "> and d2<" + d2 + ">!");
		try {
			d1 = parseDate(formatYYYYMMDD(d1));
			d2 = parseDate(formatYYYYMMDD(d2));
		} catch (ParseException e) {
			throw new RuntimeException("data format error d1=" + d1 + ",d2=" + d2);
		}

		int i = (int) ((d1.getTime() - d2.getTime()) / 86400000L);
		return i;
	}

	public static boolean checkDay(int year, int month, int monthDay) {
		if ((year < 1900) || (month < 0) || (month > 11) || (monthDay < 1) || (monthDay > 31))
			return false;
		int maxDay = getMaxDays(year, month);
		if (monthDay > maxDay)
			return false;
		return true;
	}

	public static void dealErrorDay(int year, int month, int monthDay) {
		throw new RuntimeException("The day<" + year + "," + (month + 1) + "," + monthDay + "> is ERROR-DAY!");
	}

	public static String getMonthFirstDay() {
		Date today = new Date();
		int year = getYear(today);
		int month = getMonth(today) + 1;
		int day = 1;
		return year + "-" + month + "-" + "0" + day;
	}

	public static String getCurrentDate() {
		return formatYYYYMMDD(new Date());
	}

	public static Date getCurrentDateTime() {
		return new Date();
	}

	public static void main(String[] args) {
		String dd = "2011-5-27 20:34:52";
		try {
			Date ds = parse(dd, "yyyy-MM-dd HH:mm:ss");
			System.out.println(formatYYYYMMDD(ds));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		System.out.println(format(new Date()));
	}

	/**
	 * @Title: getDateString
	 * @Description: 使用"yyyy-MM-dd HH:mm:ss"格式化日期
	 * @author Yanjh
	 * @param date
	 * @return String 返回类型
	 */
	public static String getDateString(Date date) {
		return getDateString(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * @Title: getDateString
	 * @Description: 格式化日期
	 * @author Yanjh
	 * @param date
	 *            日期
	 * @param format
	 *            模式
	 * @return String 返回类型
	 */
	public static String getDateString(Date date, String format) {
		if (date != null) {
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			String dateString = formatter.format(date);
			return dateString;
		}
		return null;
	}

	/**
	 * @Title: getDateString
	 * @Description: 使用"yyyyMMddHHmmss"格式化日期
	 * @author Wujl
	 * @param date
	 * @return String 返回类型
	 */
	public static String getDateString14(Date date) {
		return getDateString(date, "yyyyMMddHHmmss");
	}

	/**
	 * @Title: getDateString
	 * @Description: 使用"yyyy-MM-dd HH:mm:ss"格式化日期
	 * @author Wujl
	 * @param date
	 * @return String 返回类型
	 */
	public static String getDateString19(Date date) {
		return getDateString(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * @Title: getDateString
	 * @Description: 使用"yyyyMMdd"格式化日期
	 * @author Wujl
	 * @param date
	 * @return String 返回类型
	 */
	public static String getDateString8(Date date) {
		return getDateString(date, "yyyyMMdd");
	}

	/**
	 * @Title: getDateString
	 * @Description: 使用"yyyy-MM-dd"格式化日期
	 * @author HouJunChang
	 * @param date
	 * @return String 返回类型
	 */
	public static String getDateString10(Date date) {
		return getDateString(date, "yyyy-MM-dd");
	}

	/**
	 * @Title: getDateString
	 * @Description: 使用"yyyy-MM"格式化日期
	 * @author Wujl
	 * @param date
	 * @return String 返回类型
	 */
	public static String getDateString7(Date date) {
		return getDateString(date, "yyyy-MM");
	}

	/**
	 * 返回年份("获取年").
	 * 
	 * @param date
	 *            日期
	 * @return int 返回年份
	 */
	public static int getYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}

	/**
	 * 返回月份("获取月").
	 * 
	 * @param date
	 *            日期
	 * @return int 返回月份
	 */
	public static int getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * 返回日份("获取日").
	 * 
	 * @param date
	 *            日期
	 * @return int 返回日份
	 */
	public static int getDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 返回小时("获取时").
	 * 
	 * @param date
	 *            日期
	 * @return int 返回小时
	 */
	public static int getHour(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 返回分钟("获取分").
	 * 
	 * @param date
	 *            日期
	 * @return int 返回分钟
	 */
	public static int getMinute(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MINUTE);
	}

	/**
	 * 返回秒钟("获取秒").
	 * 
	 * @param date
	 *            日期
	 * @return int 返回秒钟
	 */
	public static int getSecond(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.SECOND);
	}

	/**
	 * 返回毫秒("获取毫秒").
	 * 
	 * @param date
	 *            日期
	 * @return int 返回毫秒
	 */
	public static long getMillis(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	/**
	 * 日期相加("日期增加月数").
	 * 
	 * @param date
	 *            日期
	 * @param month
	 *            月数
	 * @return 返回相加后的日期
	 */
	public static Date addMonth(Date date, int month) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, month);
		// 增加一天

		return c.getTime();
	}

	/**
	 * 日期相加.
	 * 
	 * @param date
	 *            日期
	 * @param day
	 *            天数
	 * @return 返回相加后的日期
	 */
	public static Date addDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}

	/**
	 * 日期相减.
	 * 
	 * @param date
	 *            日期
	 * @param day
	 *            天数
	 * @return 返回相减后的日期
	 */
	public static Date diffDate1(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}

	/**
	 * O 日期相减.
	 * 
	 * @param date
	 *            日期
	 * @param date1
	 *            日期
	 * @return 返回相减后的日期
	 */
	public static int diffDate(Date date, Date date1) {
		return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
	}

	/**
	 * O 时间相减.
	 * 
	 * @param date
	 *            日期
	 * @param date1
	 *            日期
	 * @return 返回相减后的时间单位秒
	 */
	public static int diffTime(Date date, Date date1) {
		return (int) ((getMillis(date) - getMillis(date1)) / 1000);
	}

	/**
	 * 日期相减.
	 * 
	 * @param date1
	 *            日期字符串
	 * @param date2
	 *            日期字符串
	 * @param format
	 *            日期字符串的格式 如：yyyyMMdd ; yyyy-MM-dd
	 * @return 返回相减后的天数
	 * @throws ParseException
	 */
	public static int diffDate(String date1, String date2, String format) throws ParseException {
		if (null == format || "" == format) {
			format = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		c1.setTime(sdf.parse(date1));
		c2.setTime(sdf.parse(date2));
		return (int) ((getMillis(c1.getTime()) - getMillis(c2.getTime())) / (24 * 3600 * 1000));
	}

	/**
	 * 日期相减("日期加减年").
	 * 
	 * @param date
	 *            日期
	 * @param years
	 *            年份
	 * @return 返回相减后的日期
	 * @throws ParseException
	 */
	public static Date addYear(Date date, int years) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(sdf.parse(sdf.format(date)));
		c.add(Calendar.YEAR, years);
		// 增加年份

		return c.getTime();
	}

	//
	/**
	 * 根据两个日期，得到两个日期之间的年月 日期字符串数组.
	 * 
	 * @param d1
	 *            日期
	 * @param d2
	 *            日期
	 * @return 返回字符型日期时间数组
	 * @throws ParseException
	 */
	public static String[] getDates(Date d1, Date d2) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar cd = Calendar.getInstance();

		ArrayList<String> stringList = new ArrayList<String>();
		while (d1.compareTo(d2) <= 0) {
			cd.setTime(sdf.parse(sdf.format(d1)));
			String dString = sdf.format(d1);
			stringList.add(dString);
			cd.add(Calendar.MONTH, 1);
			// 增加一天
			d1 = cd.getTime();
		}
		String[] arr = new String[stringList.size()];
		return stringList.toArray(arr);
	}

	/**
	 * 返回日期("js的毫秒转换为日期").
	 * 
	 * @param millis
	 *            毫秒
	 * 
	 * @return 返回日期
	 */
	public static Date jsmillis2Date(long millis) {
		Date dt = new Date(millis * 1000);
		return dt;
	}

	/**
	 * 根据年月得到该月的开始和结束日期.
	 * 
	 * @param ym
	 *            年月字符串，格式 yyyy-MM
	 * @return Object[] 首末两个时间
	 * @throws ParseException
	 */
	public static Object[] getDateYMMinAndMax(String ym) throws ParseException {
		Object[] objs = new Object[2];
		// Date dt = new Date(dt_ym + "-01");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c.setTime(sdf.parse(ym + "-01"));
		int end = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c2.setTime(sdf.parse(ym + "-" + end));

		objs[0] = c.getTime();
		objs[1] = c2.getTime();

		return objs;
	}

	/**
	 * 根据年得到该年的开始和结束日期.
	 * 
	 * @param yy
	 *            年字符串，格式 yyyy
	 * @return Object[] 首末两个时间
	 * @throws ParseException
	 */
	public static Object[] getDateYYMinAndMax(String yy) throws ParseException {
		Object[] objs = new Object[2];
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c.setTime(sdf.parse(yy + "-01-01"));
		c2.setTime(sdf.parse(yy + "-12-31"));

		objs[0] = c.getTime();
		objs[1] = c2.getTime();

		return objs;
	}

	/**
	 * 根据季度得到该季度的开始和结束日期.
	 * 
	 * @param yy
	 *            年份
	 * @param quarter
	 *            季度 1-4
	 * @return
	 * @throws ParseException
	 */
	public static Object[] getDateQuarterMinAndMax(String yy, int quarter) throws ParseException {
		Object[] objs = new Object[2];
		int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };

		int start_month = array[quarter - 1][0];
		int end_month = array[quarter - 1][2];

		Object[] dtstarts = getDateYMMinAndMax(yy + "-" + start_month);
		Object[] dtends = getDateYMMinAndMax(yy + "-" + end_month);
		objs[0] = dtstarts[0];
		objs[1] = dtends[1];
		return objs;
	}

	/**
	 * 
	 * @param y
	 * @return
	 * @throws ParseException
	 */
	public static Object[] getPentecostMinAndMax(Date y) throws ParseException {
		Object[] objs = new Object[2];
		int day = getDay(y);
		String dateStart = "";
		String dateEnd = "";
		String dateStr = getDateString(y, "yyyyMM");
		Object[] monObj = getDateYMMinAndMax(getDateString(y, "yyyy-MM"));
		String monEnd = getDateString((Date) monObj[1], "yyyyMMdd");
		if (day >= 1 && day <= 10) {
			dateStart = dateStr + "01";
			dateEnd = dateStr + "10";
		} else if (day >= 11 && day <= 20) {
			dateStart = dateStr + "11";
			dateEnd = dateStr + "20";
		} else {
			dateStart = dateStr + "21";
			dateEnd = monEnd;
		}
		objs[0] = dateStart;
		objs[1] = dateEnd;
		return objs;
	}

	/**
	 * 两个字符串的时间差.
	 * 
	 * @param begin
	 *            起始时间
	 * @param end
	 *            结束时间
	 * @return 天数
	 * @throws ParseException
	 */
	public static int countDays(String begin, String end) throws ParseException {
		int days = 0;

		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar c_b = Calendar.getInstance();
		Calendar c_e = Calendar.getInstance();

		c_b.setTime(df.parse(begin));
		c_e.setTime(df.parse(end));
		while (c_b.before(c_e)) {
			days++;
			c_b.add(Calendar.DAY_OF_YEAR, 1);
		}

		return days + 1;
	}

	/**
	 * 字符串转换为Date型.
	 * 
	 * @param date
	 *            时间
	 * @return
	 * @throws ParseException
	 */
	public static Date getData(String date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date);
	}

	public static long parseTimestamp(String str, String formate) throws ParseException {
		Date date = getData(str, formate);
		return date.getTime();
	}

	/**
	 * 字符串加天数，返回字符串.
	 * 
	 * @param time
	 *            当前时间
	 * @param day
	 *            天数
	 * @param str
	 *            日期格式
	 * @return String
	 * @throws ParseException
	 */
	public static String addDaysInString(String time, int day, String str) throws ParseException {
		Date d = getData(time, str);
		// 将字符串转换为date型
		d = addDate(d, day);
		// 加天数
		time = getDateString(d, str);
		// 将date型转换为字符串
		return time;
	}

	/**
	 * 计算当月1日到当天的天数,当年1月1日到当天的天数.
	 * 
	 * @param now
	 *            当前时间
	 * @return Object[]
	 * @throws ParseException
	 */
	public static Object[] countMonthDaysAndYearDays(String now) throws ParseException {
		// 计算当月1日到当天的天数
		int days = 0;
		Object[] objs = new Object[10];
		String begin = now.substring(0, 6) + "01";

		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar c_b = Calendar.getInstance();
		Calendar c_e = Calendar.getInstance();

		c_b.setTime(df.parse(begin));
		c_e.setTime(df.parse(now));
		while (c_b.before(c_e)) {
			days++;
			c_b.add(Calendar.DAY_OF_YEAR, 1);
		}

		objs[0] = begin;
		objs[1] = days + 1;

		// 当年1月1日到当天的天数
		days = 0;
		begin = now.substring(0, 4) + "0101";

		c_b.setTime(df.parse(begin));
		c_e.setTime(df.parse(now));
		while (c_b.before(c_e)) {
			days++;
			c_b.add(Calendar.DAY_OF_YEAR, 1);
		}

		objs[2] = begin;
		objs[3] = days + 1;
		objs[4] = now;

		return objs;
	}

	/**
	 * 计算两个日期相差的月数.
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getMonthSpace(String date1, String date2) {
		int result = 0;

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

			Calendar c1 = Calendar.getInstance();
			Calendar c2 = Calendar.getInstance();

			c1.setTime(sdf.parse(date1));
			c2.setTime(sdf.parse(date2));
			if(c2.get(Calendar.YEAR) == c1.get(Calendar.YEAR)){
				
			} else {
				
			}
			result = Math.abs(12 * (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)));
			result = result + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
		} catch (Exception e) {
			LOGGER.error(StackTraceUtil.getStackTrace(e));
		}
		return result == 0 ? 1 : Math.abs(result);

	}

	/**
	 * 根据当前日期，减去一周的日期.
	 * 
	 * @param date
	 * @return
	 */
	public static String convertWeekByDate(Date date) {
		// 设置时间格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -7);
		String imptimeEnd = sdf.format(cal.getTime());
		return imptimeEnd;
	}

	/**
	 * 根据当前日期，减去月数,计算过去的某一个月.
	 * 
	 * @param date
	 *            当前日期
	 * @param subtractor
	 *            减去月数
	 * @return
	 */
	public static String convertLastMonth(Date date, int subtractor) {
		// 设置时间格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, subtractor);
		String imptimeEnd = sdf.format(cal.getTime());
		return imptimeEnd;
	}

	/**

	 * @param startTime
	 *            开始时间(yyyy-MM-dd)
	 * @param endTime
	 *            截止时间(yyyy-MM-dd)
	 * @return
	 * @throws Exception
	 */
	public static List<String> getStartAndEndTime_Week(String startTime, String endTime) throws Exception {
		List<String> startAndEndTime = new ArrayList<String>();
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = new Date();
		Date endDate = new Date();
		Calendar calendar = Calendar.getInstance();
		if (StringUtils.isNotEmpty(startTime) && startTime.length() == 10
				&& StringUtils.isNotEmpty(endTime) && endTime.length() == 10) {
			/*
			 * 第一步：找到起始日期
			 */
			startDate = sdFormat.parse(startTime);
			calendar.setTime(startDate);
			int startWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
			/*
			 * 如果不是星期一，将日期向后移，直到找到第一个星期一为止，并将日期设置为起始日期
			 */
			while (Calendar.MONDAY != startWeekDay) {
				calendar.add(Calendar.DAY_OF_YEAR, 1);
				startWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
			}
			startDate = calendar.getTime();
			/*
			 * 第二步：找到截止日期
			 */
			endDate = sdFormat.parse(endTime);
			calendar.setTime(endDate);
			int endWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
			/*
			 * 如果截止日期不是星期天，那么将日期向前移，直到找到第一个星期天为止，并将日期设置 为截止日期
			 */
			while (Calendar.SUNDAY != endWeekDay) {
				calendar.add(Calendar.DAY_OF_YEAR, -1);
				endWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
			}
			endDate = calendar.getTime();
		}
		if (endDate.before(startDate)) {
			String errorString = "false";
			startAndEndTime.add(errorString);
		} else {
			startAndEndTime.add(sdFormat.format(startDate));
			startAndEndTime.add(sdFormat.format(endDate));
		}
		return startAndEndTime;
	}
	/**
	 * 获取一个月的最后一天
	 * @param YYYYMM
	 * @return
	 */
	public static String getLastdayOfMonth(String YYYYMM){
		Calendar cal = Calendar.getInstance();
	    //设置年份
        cal.set(Calendar.YEAR,Integer.parseInt(YYYYMM.substring(0,4)));
        //设置月份
        cal.set(Calendar.MONTH, Integer.parseInt(YYYYMM.substring(5))-1);
        //最大日期
        cal.set(Calendar.DATE,1);
        cal.roll(Calendar.DATE,-1);
        //获取某月最大天数
        //int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        int lastDay = cal.get(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String lastDayOfMonth = sdf.format(cal.getTime());
         
        return lastDayOfMonth;
		
	}
}
